<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>领用单管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="../css/base.css">
    <link rel="stylesheet" href="../../css/element-ui.css" />
    <link rel="stylesheet" href="../css/index.css">
    <!--[if lt IE 9]>
    <link rel="stylesheet" href="../css/pages/ie8.css"/>
    <script src="../../js/common/html5shiv.min.js"></script>
    <script src="../../js/common/respond.min.js"></script>
    <script src="../../js/common/es5-shim.min.js"></script>
    <![endif]-->
    <script src="../../js/common/jquery.min.js"></script>
    <script src="../../js/common/browser.min.js"></script>
    <script src="../../js/common/vue.js"></script>
    <script src="../../js/common/element-ui.js"></script>
    <script src="../js/common/common.js"></script>
</head>

<body>
    <div id="wtly" class="container">
        <div class="rj-header">
            <p class="nav">
                <span class="default">您现在所在的位置:</span>
                <span class="direction">{{prams.breadcrumb}}</span>
            </p>
            <div class="search-container">
                <div class="item">
                    <label>录入时间</label>
                    <div class="time-wrapper">
                        <el-date-picker v-model="inputTimeStart" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择起始时间">
                        </el-date-picker>
                    </div>
                    <span>---</span>
                    <div class="time-wrapper">
                        <el-date-picker v-model="inputTimeEnd" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择结束时间">
                        </el-date-picker>
                    </div>
                </div>
                <div class="item">
                    <label>入库科室账户：</label>
                    <input type="text" class="forbid" disabled="disabled" :value="InDeptAccount.Ksmc" />
                </div>
                <div class="item">
                    <label>单据状态</label>
                    <select v-model="StatusStr">
          <option value="all">全部</option>
          <option value="lr">已录入</option>
          <option value="fs">已发送</option>
          <option value="sh">已审核</option>
          <option value="rk">已登账</option>
          <option value="jz">已结账</option>
          <option value="zf">已作废</option>
        </select>
                </div>
                <div class="item">
                    <label>来源类型：</label>
                    <select v-model="AdditionalReasonType">
          <option value="all">全部</option>
          <option value="zc">正常</option>
          <option value="cf">冲负</option>
          <option value="bcj">补差价</option>
        </select>
                </div>

                <div class="item">
                    <el-button size="small" type="primary" @click="search">查询</el-button>
                    <el-button size="small" type="primary" @click="jumpToNewBill()">新建单据</el-button>
                    <el-button size="small" type="info" @click="back">返回</el-button>
                </div>
            </div>
        </div>

        <div class="table-wrapper">

            <el-table :data="BillData" border style="width: 1450px" height="400">
                <el-table-column prop="BillSN" label="单据流水号" align="center" width="180">
                </el-table-column>
                <el-table-column prop="InputTime" label="录入时间" align="center" show-overflow-tooltip width="200">
                </el-table-column>
                <el-table-column prop="AcceptTime" label="登账日期" align="center" show-overflow-tooltip width="200">
                </el-table-column>
                <el-table-column prop="StatusName" label="单据状态" align="center" width="120">
                </el-table-column>
                <el-table-column prop="InDeptName" label="领用科室" align="center" width="120">
                </el-table-column>
                <el-table-column prop="InputUserName" label="录入人" align="center" width="120">
                </el-table-column>
                <el-table-column prop="AuditUserName" label="审核人" align="center" width="120">
                </el-table-column>
                <el-table-column prop="AcceptUserName" label="登账人" align="center" width="120">
                </el-table-column>
                <el-table-column fixed="right" align="center" label="操作" width="200">
                    <template scope="scope"> 
     	<el-button type="text" @click="handleClick(scope.row)" size="small">查看</el-button>
     	<el-button type="text" size="small"  @click="handleClick(scope.row)" :disabled="!scope.row.operationRights.isEditor">修改</el-button>
     	<el-button type="text" size="small"  @click="sendClick(scope.row)"  :disabled="!scope.row.operationRights.isSend">发送</el-button>
     	<el-button type="text" size="small"  @click="handleDel(scope.row)" :disabled="!scope.row.operationRights.isZf">作废</el-button>
      </template>
                </el-table-column>
            </el-table>
        </div>
    </div>
    <script>
        var app = new Vue({
            el: '#wtly',
            data: {
                prams: JSON.parse(GetRequest().param),
                userJS: JSON.parse(localStorage.getItem("userJS")),
                InDeptAccount: JSON.parse(GetRequest().param).InDeptAccount,
                OutDeptAccount: {},
                isUserJS: localStorage.getItem("userJS") && JSON.parse(localStorage.getItem("userJS")).length > 0 ? true : false,
                inputTimeStart: getDay(-3) + " 00:00:00",
                inputTimeEnd: getDay(0) + " 23:59:59",
                userID: JSON.parse(localStorage.getItem("currentUser")).Ygid,
                BillData: [],
                StatusStr: 'all',
                AdditionalReasonType: 'all'
            },
            created: function() {
                var that = this;
                console.log(JSON.parse(GetRequest().param))
                this.GetOutDTODept().then(function(data) {
                    that.OutDeptAccount = data
                    that.getBills();
                })
            },

            methods: {
                toName: function(value) {
                    if (!value) return ''
                    value = value.toString()
                    switch (value) {
                        case "lr":
                            value = "已录入"
                            break;
                        case "fs":
                            value = "已发送"
                            break;
                        case "sh":
                            value = "已审核"
                            break;
                        case "ly":
                            value = "已完成"
                            break;
                        case "rk":
                            value = "登账"
                            break;
                        case "jz":
                            value = "结账"
                            break;
                        case "zf":
                            value = "已作废"
                            break;
                    }
                    return value
                },
                operationRights: function(value) {
                    if (!value) return ''
                    value = value.toString()
                    var operations = {}
                    switch (value) {
                        case "lr":
                            operations = {
                                isEditor: true,
                                isSend: true,
                                isZf: true
                            }
                            break;

                        case "fs":
                            operations = {
                                isEditor: true,
                                isSend: false,
                                isZf: true
                            }
                            break;

                        case "sh":
                            operations = {
                                isEditor: false,
                                isSend: false,
                                isZf: false
                            }
                            break;
                        case "rk":
                            operations = {
                                isEditor: false,
                                isSend: false,
                                isZf: false
                            }
                            break;
                        case "zf":
                            operations = {
                                isEditor: false,
                                isSend: false,
                                isZf: false
                            }
                            break;
                    }
                    return operations
                },
                handleClick: function(o) {
                    window.location.href = "wtlynewbill.html?param=" + encodeURI(JSON.stringify(this.prams)) + '&BillSN=' + o.BillSN + '&OutDept=' + encodeURI(JSON.stringify(this.OutDeptAccount));
                },
                sendClick: function(o) {
                    var that = this;
                    var parms = {
                        className: 'TwoLevelStockManager',
                        method: 'SendDrawBill',
                        billSN: o.BillSN,
                        userID: this.userID
                    }
                    this._fetch(parms).then(function(data) {
                        that.getBills();
                        that.$message({
                            message: '发送成功',
                            type: 'success'
                        });
                    })
                },
                handleDel: function(o) {
                    var that = this
                    var parms = {
                        className: 'DrawBillManager',
                        method: 'NullifyDrawBill',
                        billSN: o.BillSN,
                        userID: this.userID
                    }
                    this._fetch(parms).then(function(data) {
                        that.getBills();
                        that.$message({
                            message: '作废成功',
                            type: 'success'
                        });
                    })

                },
                search: function() {
                    this.getBills()
                },
                back: function() {
                    parent.showMenuFn()
                    parent.$('#main').empty()
                },
                jumpToNewBill: function() {
                    window.location.href = "wtlynewbill.html?param=" + encodeURI(JSON.stringify(this.prams)) + '&OutDept=' + encodeURI(JSON.stringify(this.OutDeptAccount));
                },
                conditionModel: function(ColumnName, FullString, Oper, Value, Type) {

                    var obj = {
                        "ColumnName": ColumnName,
                        "FullString": FullString,
                        "Oper": Oper,
                        "Value": Value,
                        "Type": Type
                    }
                    return obj;
                },
                conditionsModel: function(Conditions, Type) {
                    var obj = {
                        "Conditions": Conditions,
                        "Type": Type
                    }
                    return obj
                },
                GetOutDTODept: function() {
                    var that = this
                        //var ksdm = localStorage.getItem('ksdm')
                    var parms = {
                        className: "OrganizationManager",
                        method: 'GetOutDTODept',
                        ksdm: "0030100"
                    }
                    return this._fetch(parms);
                },
                getBills: function() {
                    var that = this
                    var arr = [];
                    var condition;
                    var inputTimeStart = typeof this.inputTimeStart == "object" ? this.inputTimeStart.format("yyyy-MM-dd hh:mm:ss") : this.inputTimeStart;
                    var inputTimeEnd = typeof this.inputTimeEnd == "object" ? this.inputTimeEnd.format("yyyy-MM-dd hh:mm:ss") : this.inputTimeEnd
                    if (!inputTimeStart || !inputTimeEnd) {
                        that.$message.error('时间必选！');
                        return false
                    }

                    var InDept = this.conditionModel("InDeptAccountID", null, "Equal", this.prams.InDeptAccountID, "String")
                    var OutDept = this.conditionModel("OutDeptAccountID", null, "Equal", this.OutDeptAccount.DeptAccountID, "String")
                    var Depts = [InDept, OutDept]
                    var DeptsCondition = this.conditionsModel(Depts, "AND")
                    var StartTime = this.conditionModel("mb.InputTime", null, "MoreOrEqual", inputTimeStart, "DateTime")
                    var EndTime = this.conditionModel("mb.InputTime", null, "Less", inputTimeEnd, "DateTime")
                    var Bt = this.conditionModel("BillType", null, "Equal", "ly", "String")
                    var GBId = this.conditionModel("GoodsBookID", null, "Equal", JSON.parse(GetRequest().param).Openparm.GoodsBook, "String")

                    var sourse = this.AdditionalReasonType
                    var oSourse = this.conditionModel("AdditionalReasonType", null, "Equal", sourse, "String")
                    var Status = this.StatusStr
                    var oStatusStr = this.conditionModel("Status", null, "Equal", Status, "String")
                    var isAlls = [sourse, Status];
                    var conditions = [oSourse, oStatusStr];
                    arr = [DeptsCondition, StartTime, EndTime, Bt, GBId]
                    for (var i = 0; i < conditions.length; i++) {
                        if (isAlls[i] !== 'all') {
                            arr = arr.concat([conditions[i]])
                        }
                    }
                    condition = this.conditionsModel(arr, "AND")
                    var parms = {
                        className: 'MoveBillQueryManager',
                        method: 'GetBills',
                        condition: JSON.stringify(condition)
                    }
                    this._fetch(parms).then(function(data) {
                        var data = data
                        if (data.length > 0) {
                            data = data.map(function(item) {
                                //item.StatusStr = that.toName(item.StatusStr)
                                that.$set(item, "StatusName", that.toName(item.StatusStr))
                                that.$set(item, "operationRights", that.operationRights(item.StatusStr))
                                return item
                            })
                        }
                        that.BillData = data
                    })
                },
                _fetch: function(parms) {
                    return new Promise(function(resolve, reject) {
                        $.ajax({
                            type: 'POST',
                            url: getRootPath() + '/api/sendPostRequest',
                            headers: {
                                'Accept': 'application/json',
                                'Content-Type': 'application/json'
                            },
                            data: JSON.stringify(parms),
                            success: function(res) {
                                if (res.isSessionInvalidated) {
                                    parent.window.location.href = getRootPath() + 'login.html'
                                }
                                if (res.issuccess == 0) {
                                    var result = res.result.match(/^[ ]*$/) ? res.result : JSON.parse(res.result);
                                    resolve(result)
                                } else {
                                    reject(res.Message);
                                }
                            },
                            error: function(err) {
                                new Error(err)
                            }
                        });
                    });
                }


            }
        })
    </script>
</body>

</html>